package com.xdtech.cc.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.cc.dao.AccountDao;
import com.xdtech.cc.model.Account;
import com.xdtech.cc.service.AccountService;
import com.xdtech.cc.vo.AccountItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2016-05-29 21:53:30
 * @since 1.0
 * @see
 */
@Service
public class AccountServiceImpl implements AccountService {
	private Log log = LogFactory.getLog(AccountServiceImpl.class);
	@Autowired
	private AccountDao accountDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param entity
	 */
	public void save(Account entity) {
		accountDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Account> entities) {
		accountDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param entity
	 */
	public void delete(Account entity) {
		accountDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		accountDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param id
	 * @return
	 */
	public Account get(Long id) {
		return accountDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @return
	 */
	public List<Account> getAll() {
		return accountDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> accounts = null;
		long rows = 0;
		if (pg!=null) {
			Page<Account> page = accountDao.findPage(pg,"from Account order by createTime desc", values);
			accounts = BeanUtils.copyListProperties(
					AccountItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Account> accountList = accountDao.find("from Account order by id desc", values);
			accounts = BeanUtils.copyListProperties(
					AccountItem.class, accountList);
			rows = accounts.size();
		}
		results.put("total", rows);
		results.put("rows", accounts);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateAccount(AccountItem item) {
		Account account = null;
		if (item.getId()==null) {
			account = new Account();
		}else {
			account = accountDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(account, item);
		accountDao.save(account);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param accountId
	 * @return
	 */
	public AccountItem loadAccountItem(Long accountId) {
		Account account = accountDao.get(accountId);
		return AccountItem.createItem(account);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteAccountInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteAccountInfo(List<Long> accountIds) {
		for (Long id : accountIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:30
	 * @modified by
	 * @return
	 */
	@Override
	public List<AccountItem> loadItems() {
		List<Account> accounts = getAll();
		return AccountItem.createItems(accounts);
	}

}
